﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Gpsys.Web.Models;
using Gpsys.Web.Common;

namespace Gpsys.Web.Controllers
{
    public class UserController : BaseController
    {
        private void SetSelectedMenu()
        {
            ViewBag.SelectedMenu = "User";
        }

        //
        // GET: /User/

        public ActionResult Index()
        {
            SetSelectedMenu();
            UserModel wfModel = new UserModel();
            return View(wfModel);
        }

        public ActionResult Create()
        {
            SetSelectedMenu();
            ViewBag.FormTitle = "Create User";
            ViewBag.IsUpdate = false;
            UserEntityModel model = new UserEntityModel()
            {
                User = new Entity.MSG.User()
            };
            return View("Edit", model);
        }

        [HttpPost]
        public ActionResult Create(UserEntityModel model)
        {
            SetSelectedMenu();
            ViewBag.FormTitle = "Create User";
            ViewBag.Valid = true;
            ViewBag.IsUpdate = false;

            UserModel wfModel = new UserModel();

            wfModel.ValidateUser(ModelState, model, true);

            ViewBag.Valid = ModelState.IsValid;

            if (ModelState.IsValid)
            {
                if (wfModel.InsertUser(model.User))
                {
                    return RedirectToAction("Index");
                }
            }

            return View("Edit", model);
        }

        [HttpPost]
        public ActionResult Edit(UserEntityModel model)
        {
            SetSelectedMenu();
            ViewBag.FormTitle = "Edit User";
            ViewBag.Valid = true;
            ViewBag.IsUpdate = true;

            UserModel wfModel = new UserModel();

            wfModel.ValidateUser(ModelState, model, false);

            ViewBag.Valid = ModelState.IsValid;

            if (ModelState.IsValid)
            {
                if (wfModel.UpdateUser(model.User))
                {
                    return RedirectToAction("Index");
                }
            }

            return View("Edit", model);
        }

        public ActionResult Edit(int Id)
        {
            SetSelectedMenu();
            ViewBag.FormTitle = "Edit User";

            ViewBag.IsUpdate = true;

            UserModel wfModel = new UserModel();
            var wf = wfModel.GetUser(Id);
            if (wf != null)
            {
                UserEntityModel m = new UserEntityModel()
                {
                    User = wf
                };
                return View("Edit", m);
            }

            return RedirectToAction("e404", "Error");
        }

        public ActionResult Detail(int Id)
        {
            SetSelectedMenu();
            ViewBag.FormTitle = "Detail User";

            UserModel wfModel = new UserModel();
            var wf = wfModel.GetUser(Id);
            if (wf != null)
            {
                UserEntityModel m = new UserEntityModel()
                {
                    User = wf
                };
                return View(m);
            }

            return RedirectToAction("e404", "Error");
        }

        public ActionResult Delete(int Id)
        {
            SetSelectedMenu();

            UserModel wfModel = new UserModel();

            var wf = wfModel.GetUser(Id);
            if (wf != null)
            {
                wfModel.DeleteUser(Id);

                return RedirectToAction("Index");
            }

            return RedirectToAction("e404", "Error");
        }

    }
}
